<div class="layuimini-main">
    <div class="layui-form layuimini-form" lay-filter="pageruleForm">
        <input type="hidden" id="taskId" name="taskId" value="${taskId}">
        <input type="hidden" id="parentPageRuleId" name="parentPageRuleId" value="${parentPageRuleId}">
        <input type="hidden" id="parentLevel" name="parentLevel" value="${parentLevel}">
        <input type="hidden" id="detailRule" name="detailRule" value="">
        <div class="layui-form-item">
            <label class="layui-form-label">模板信息:</label>
            <div class="layui-input-block">
                <div class="layui-form-mid layui-word-aux" id="readonlyTaskName">${taskId}-${taskName}</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">父页面规则:</label>
            <div class="layui-input-block">
                <div class="layui-form-mid layui-word-aux" id="readonlyParentRuleName">${parentPageRuleId}
                    -${parentPageRuleName}</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">页面名称</label>
            <div class="layui-input-block">
                <input type="text" name="ruleName" lay-verify="required" lay-reqtext="页面名称不能为空" placeholder="请输入页面名称"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">页面类型</label>
            <div class="layui-input-block">
                <input type="radio" name="pageType" value="1" title="列表页" checked="" lay-filter="pageType">
                <input type="radio" name="pageType" value="2" title="详情页" lay-filter="pageType">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">内容类型</label>
            <div class="layui-input-block">
                <input type="radio" name="pageContentType" value="1" title="html" checked=""
                       lay-filter="pageContentType">
                <input type="radio" name="pageContentType" value="2" title="json" lay-filter="pageContentType">
                <input type="radio" name="pageContentType" value="3" title="xml" lay-filter="pageContentType">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">匹配类型</label>
            <div class="layui-input-block">
                <input type="radio" name="patternType" value="1" title="xpath" checked="">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn" id="addPageRule">确认保存
                </button>
                <#--                <button type="reset" class="layui-btn layui-btn-primary" id="resetBtn">重置</button>-->
            </div>
        </div>
    </div>
</div>
<script>

    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            miniPage = layui.miniPage,
            $ = layui.$;

        // 判断是否是edit模式
        var url = "/pageRule/createPageRule";
        var pageRuleId = "";
        <#if pageRuleDO??>
        form.val("pageruleForm", {
                "ruleName": "${pageRuleDO.ruleName}",
                "pageType": "${pageRuleDO.pageType}",
                "pageContentType": "${pageRuleDO.pageContentType}",
                "patternType": "1"
            }
        );
        url = "/pageRule/updatePageRule";
        pageRuleId = "${pageRuleDO.id}";
        </#if>

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        form.on('radio(pageType)', function (data) {
            if (data.value == 1) {
            } else {
            }
        })

        // $("#resetBtn").click(function(){
        //     form.render();
        // })

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        form.on('submit(cancelBtn)', function (data) {
            // 关闭弹出层
            layer.close(parentIndex);
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var urlRule = {
                parseType: data.field.patternType
            }

            var requestData = {
                taskId: data.field.taskId,
                parentRuleId: data.field.parentPageRuleId,
                ruleName: data.field.ruleName,
                pageType: data.field.pageType,
                pageContentType: data.field.pageContentType,
                level: parseInt(data.field.parentLevel) + 1,
                urlRule: JSON.stringify(urlRule),
                pageRuleId: pageRuleId
            }

            $.ajax({
                url: url,
                type: 'post',
                data: requestData,
                success: function (data) {
                    if (data.code == 100) {
                        $("#addPageRule").attr("layuimini-content-href", "/pageRule/showPageRulesSetting?taskId=" + taskId);
                        // 关闭弹出层
                        layer.close(parentIndex);
                    } else {
                        return layer.msg('Code:' + data.code + '，' + data.msg + '，请稍后再试！');
                    }
                },
                error: function (xhr, textstatus, thrown) {
                    return layer.msg('Status:' + xhr.status + '，' + xhr.statusText + '，请稍后再试！');
                }
            });
            return false;
        });

    });
</script>